package tutor;

import static org.lwjgl.opengl.GL11.*;

import java.awt.Color;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

public class Tutorial3 extends Game {

	int x = 150;
	int y = 150;
	int rotation = 0;

	public void init() {
		Display.setTitle("Test title");

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		glMatrixMode(GL_MODELVIEW);
		glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
	}

	public void update(long elapsedTime) {
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
			x -= 4;

		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
			x += 4;

		if (Keyboard.isKeyDown(Keyboard.KEY_UP))
			y -= 4;

		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
			y += 4;

		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
			end();
		
		rotation += 4;
	}

	public void render() {
		glClear(GL_COLOR_BUFFER_BIT);
		glPushMatrix();
		{
		    // Translate to the location.
		    glTranslatef(x, y, 0);

		    // Translate to the center of triangle
		    glTranslatef(50, 50, 0);

		    // Rotate on the z-axis with 1-degree.
		    glRotatef(rotation, 0, 0, 1);

		    // Translate back to the first coord.
		    glTranslatef(-50, -50, 0);

		    // Draw a triangle.
		    glBegin(GL_TRIANGLES);
		    {
		        // Draw first vertex in red
		        glColor3f(1, 0, 0);
		        glVertex2f(0, 0);

		        // Draw second vertex in green.
		        glColor3f(0, 1, 0);
		        glVertex2f(0, 100);

		        // Draw third vertex in blue.
		        glColor3f(0, 0, 1);
		        glVertex2f(100, 100);
		    }
		    glEnd();
		}
		glPopMatrix();
	}

	public void resized() {
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
	}

	public static void main(String[] args) {
		new Tutorial3();
	}

	public static void setAwtColor(Color c) {
		float red = c.getRed() / 255f;
		float green = c.getGreen() / 255f;
		float blue = c.getBlue() / 255f;
		float alpha = c.getAlpha() / 255f;

		glColor4f(red, green, blue, alpha);
	}
}
